package com.sc.search.service.impl;

import com.sc.search.Dao.SearchDao;
import com.sc.common.pojo.SearchResult;
import com.sc.search.service.SearchService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @auther sc
 * @Date 2017/10/26
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private SearchDao searchDao;

    public SearchResult search(String queryString,int page,int rows) throws SolrServerException {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery(queryString);
        solrQuery.setStart((page-1)*rows);
        solrQuery.setRows(rows);
        solrQuery.set("df","item_title");
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("item_title");
        solrQuery.setHighlightSimplePre("<font class='skcolor_ljg'>");
        solrQuery.setHighlightSimplePost("</font>");
        SearchResult result = searchDao.search(solrQuery);
        Long recordCount = result.getRecordCount();
        int pageCount = (int) (Math.ceil(recordCount/rows));
        result.setPageCount(pageCount);
        result.setCurrPage(page);
        return result;
    }
}
